// 定义子路由 
const express = require('express');
const mysql = require('mysql')
const pool = require("../db");
const Video = express.Router();

// 所有视频接口 /video
Video.get('/', (req, res, next) => {
	pool.query('select * from video', (err, results) => {
		if (err) {
			return next(err)
		}
		// console.log(results,'results')
		if (results.length === 0) {
			res.send({ code: 201, msg: '暂无' })
		} else {
			res.send({ code: 200, msg: '查询成功', data: results })
		}
	})
})

// 视频详情页接口 /video/details?vid=
Video.get('/details', (req, res, next) => {
	let obj = req.query.vid
	console.log(obj);
	pool.query('select * from video where vid=?', [obj], (err, results) => {
		if (err) {
			return next(err)
		}
		if (results.length === 0) {
			res.send({ code: 201, msg: '暂无' })
		} else {
			res.send({ code: 200, msg: '查询成功', data: results })
		}
	})
})

// 收藏接口
Video.post('/collection', (req, res, next) => {
	let uid = req.body.uid
	let vid = req.body.vid
	pool.query('insert into user_video(uid,vid) values(?,?)', [uid, vid], (err, results) => {
		if (err) {
			return next(err)
		}
		console.log(results);
		if (results.affectedRows == 0) {
			res.send({ code: 201, msg: '暂无' })
		} else {
			res.send({ code: 200, msg: '成功', data: results })
		}
	})
})

// 取消收藏接口
Video.delete('/cancel', (req, res, next) => {
	let uid = req.query.uid
	let vid = req.query.vid
	pool.query('delete from user_video where uid=? and vid=?', [uid, vid], (err, results) => {
		if (err) {
			return next(err)
		}
		console.log(results);
		if (results.affectedRows == 0) {
			res.send({ code: 201, msg: '失败' })
		} else {
			res.send({ code: 200, msg: '取消收藏成功', data: results })
		}
	})
})

// 用户有没有收藏视频
Video.post('/select', (req, res, next) => {
	let uid = req.body.uid
	let vid = req.body.vid
	pool.query('select * from user_video where uid=? and vid=?', [uid, vid], (err, results) => {
		if (err) {
			return next(err)
		}
		console.log(results.length);
		if (results.length === 0) {
			res.send({ code: 201, msg: '未收藏' })
		} else {
			res.send({ code: 200, msg: '已收藏' })
		}
	})
})


// 我的收藏接口
Video.post('/mycol', (req, res, next) => {
	let uid = req.body.uid
	let sql = 'select * from video where vid in(select vid from user_video where uid=?)'
	pool.query(sql, [uid], (err, results) => {
		if (err) {
			return next(err)
		}
		console.log(results[0]);
		if (results.length === 0) {
			res.send({ code: 201, msg: '暂无收藏' })
		} else {
			res.send({ code: 200, msg: '查询成功', data: results })
		}
	})
})

// 发布评论的接口
Video.post('/comment', (req, res, next) => {
	let comment = req.body.comment
	let time = req.body.time
	let userName = req.body.userName
	let pic = req.body.pic
	let vid = req.body.vid
	let uid = req.body.uid
	pool.query('insert into comment(comment,time,userName,pic,vid,uid) values(?,?,?,?,?,?)', [comment, time, userName, pic, vid,uid], (err, results) => {
		if (err) {
			return next(err)
		}
		// console.log(results);
		if (results.affectedRows == 0) {
			res.send({ code: 400, msg: '失败' })
		} else {
			res.send({ code: 200, msg: '成功', data: results })
		}
	})
})

// 查询评论接口
Video.get('/selComment', (req, res, next) => {
	let vid = req.query.vid
	pool.query('select * from comment where vid=?', [vid], (err, results) => {
		if (err) {
			return next(err)
		}
		console.log(results[0]);
		if (results.length === 0) {
			res.send({ code: 201, msg: '暂无评论' })
		} else {
			res.send({ code: 200, msg: '查询成功', data: results })
		}
	})
})

// 修改评论用户的用户名及头像
Video.post('/change',(req,res,next)=>{
	//获取post传递的参数
	let username = req.body.username
	let pic = req.body.pic
	let uid = req.body.uid
	pool.query('update comment set username=?,pic=? where uid=?',[username,pic,uid],(err,results)=>{
		if (err) {
			return next(err)
		}
		if (results.affectedRows === 0) {
			res.send({ code: 201, msg: '修改失败' })
		} else {
			res.send({ code: 200, msg: '修改成功' })
		}
	})
})

module.exports = Video